package com.example.demo.service.Impl;

import com.example.demo.dto.TouristListDTO;
import com.example.demo.entity.Attraction;
import com.example.demo.entity.RecordCount;
import com.example.demo.mapper.AttractionMapper;
import com.example.demo.mapper.ParkingLotMapper;
import com.example.demo.service.AttractionService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @program: demo
 * @description:
 * @author: Lelouch
 * @create: 2024-09-04 15:42
 **/
@Service
public class AttractionServiceImpl implements AttractionService {
    @Resource
    AttractionMapper attractionMapper;
    @Resource
    ParkingLotMapper parkingLotMapper;

    @Override
    public PageInfo<Attraction> list() {
        List<Attraction> attractions = attractionMapper.list();
        PageInfo<Attraction> attractionPageInfo = new PageInfo<>(attractions);
        return attractionPageInfo;
    }

    @Override
    public PageInfo<Attraction> listByParams(Integer currentPage,Integer pageSize) {
        PageHelper.startPage(currentPage,pageSize);
        List<Attraction> attractions = attractionMapper.listByParams();
        PageInfo<Attraction> attactionsPageInfo = new PageInfo<>(attractions);
        return attactionsPageInfo;
    }

    @Override
    public int addAttraction(Attraction attraction){
        return attractionMapper.add(attraction);
    }

    @Override
    public boolean changeAttraction(Attraction attraction) {
        return attractionMapper.changeAttraction(attraction);
    }

    @Override
    public Attraction getAttractionById(Integer id) {
        return attractionMapper.getAttractionById(id);
    }

    @Override
    public boolean deleteAttractionById(Integer id) {
        Integer parkingLotId = parkingLotMapper.selectByAttractionId(id);
        return attractionMapper.deleteById(id) && parkingLotMapper.deleteByID(id, parkingLotId);
    }

    @Override
    public PageInfo<RecordCount> touristList(TouristListDTO touristListDTO) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date1 = null;
        Date date2 = null;
        if(touristListDTO.getStartDate()!=null){
            try {
                date1 = format.parse(touristListDTO.getStartDate()); // 解析为 Date 对象
            }catch (ParseException e) {
                System.err.println("Error parsing date: " + e.getMessage());
            }
        }
        if(touristListDTO.getEndDate()!=null){
            try {
                date2 = format.parse(touristListDTO.getStartDate()); // 解析为 Date 对象
            }catch (ParseException e) {
                System.err.println("Error parsing date: " + e.getMessage());
            }
        }
        PageHelper.startPage(touristListDTO.getCurrentPage(),touristListDTO.getPageSize());
        List<RecordCount> recordCounts = attractionMapper.touristList(touristListDTO.getAttractionId(), date1, date2);
        PageInfo<RecordCount> recordCountPageInfo = new PageInfo<>(recordCounts);
        return recordCountPageInfo;
    }

    @Override
    public Integer selectByAttractionName(String name) {
        return attractionMapper.selectIdByName(name);
    }
}
